package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.dto.QueryPageBeanDTO;
import com.itheima.health.pojo.entity.Setmeal;
import com.itheima.health.pojo.result.PageResult;
import com.itheima.health.pojo.result.Result;
import com.itheima.health.service.SetmealService;
import com.itheima.health.util.AliossUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliossUtil aliossUtil;

    @GetMapping("/findPage")
    public Result findPage(QueryPageBeanDTO queryDto){
        PageResult page = setmealService.findPage(queryDto);
        return new Result(true, MessageConst.ACTION_SUCCESS, page);
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file) throws Exception {
        String url = aliossUtil.upload(file.getOriginalFilename(), file.getInputStream());
        return new Result(true, MessageConst.ACTION_SUCCESS, url);
    }

    @PostMapping("/add")
    public Result add(@RequestParam("checkgroupIds")List<Integer> ids, @Validated @RequestBody Setmeal setmeal){
        setmealService.add(ids, setmeal);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }
}
